/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CallTables.java
 *
 * Created on 31/08/2011, 23:12:13
 */

package ufpb.compiladores.lexico.run.GUI;

import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.filechooser.FileFilter;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import ufpb.compiladores.sintatico.SyntacticalAnalyzer;

/**
 *
 * @author Sarah
 */
public class CallTables extends javax.swing.JFrame {

    private boolean syntactical = false;
    
    public CallTables (boolean synt) throws IOException{

        this.syntactical = synt;
        this.initComponents();
        //jtp = new JTabbedPane();
        //this.add(jtp);
        //this.getRootPane().add(jtp);
        if (!synt) {
            
            this.jMenu2.remove(this.jMenuItem2);
        }
        this.setSize(1100,650);
        this.setLocationRelativeTo(null);
    }
    
    public CallTables (Document[] doc, File [] file, SyntacticalAnalyzer[] sa) throws IOException{

        this(sa != null);
        
        /*Object[] objectTabela = new Object[3];
        Object[] objectErros = new Object[2];*/

        
        this.setSize(1100,650);
        this.setLocationRelativeTo(null);
        
        Panel p = null;
        for(int i=0; i<doc.length; i++){ //chamar tables pra preencher, passar cada arquivo e lá preenche

            p = new Panel(file[i], this.syntactical);
            //p.addDocsAndFiles(doc, file);
            //p.preencheTextArea(file[i]); //Colocar as linhas
            //p.preencheNomeDoArquivo(file[i].getAbsolutePath());

            //jtp.addTab(file[i].getName(),p);
            p.setTabbedNumber(i);
            this.jTabbedPane1.addTab(file[i].getName(),p);            
       }
    }
    
    /*public CallTables(SyntacticalAnalyzer[] sa, Document[] docs, File[] files) throws IOException {
        
        this(docs, files);
        this.sa = sa;
    }*/



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jFrame2 = new javax.swing.JFrame();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
        jFrame2.getContentPane().setLayout(jFrame2Layout);
        jFrame2Layout.setHorizontalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame2Layout.setVerticalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jMenu1.setText("Arquivo");

        jMenuItem4.setText("Novo arquivo em branco");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem3.setText("Adicionar arquivo");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Configurações");

        jMenuItem1.setText("Arquivo de configuração do léxico");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem2.setText("Arquivo de configuração do sintático");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1100, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 623, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        
        JFileChooser fc = new JFileChooser();
        MyFileFilter filter = new MyFileFilter();
        fc.setFileFilter(filter);

        if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

            try {
                
                ufpb.compiladores.lexico.XMLLoader loader
                        = new ufpb.compiladores.lexico.XMLLoader(fc.getSelectedFile());
                Instances.LEXICAL_ENVIROMENT = loader.createEnviroment();
            } catch (Exception e) {
                
                JOptionPane.showMessageDialog(this, "Erro ao carregar arquivo de configuração",
                        "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        
        JFileChooser fc = new JFileChooser();
        MyFileFilter filter = new MyFileFilter();
        fc.setFileFilter(filter);

        if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

            try {
                
                ufpb.compiladores.sintatico.XMLLoader loader
                        = new ufpb.compiladores.sintatico.XMLLoader(fc.getSelectedFile());
                Instances.SYNTACTICAL_ENVIROMENT = loader.createEnviroment();
                Instances.SYNTACTICAL_ENVIROMENT.removeRecursion();
                Instances.SYNTACTICAL_ENVIROMENT.removeNonDeterminism();
            } catch (Exception e) {
                
                JOptionPane.showMessageDialog(this, "Erro ao carregar arquivo de configuração",
                        "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        
        Panel p = new Panel(this.syntactical);
        p.setVisible(true);
        //this.jtp.add("New Document", p);
        this.jTabbedPane1.addTab("New File", p);
        //this.jtp.addTab("435", null, p, "2w");
      
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        
        JFileChooser fc = new JFileChooser();
        MyFileFilter2 filter = new MyFileFilter2();
        fc.setFileFilter(filter);

        if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

            try {
                
                Panel p = new Panel(fc.getSelectedFile(), this.syntactical);
                this.jTabbedPane1.addTab(fc.getSelectedFile().getAbsolutePath(), p);
            } catch (Exception e) {
                
                JOptionPane.showMessageDialog(this, "Erro ao carregar o arquivo",
                        "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
        
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFrame jFrame1;
    private javax.swing.JFrame jFrame2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

    private static class MyFileFilter extends FileFilter{

        @Override
        public boolean accept(File pathname) {

            if((pathname.getName().endsWith("xml"))|| pathname.isDirectory()){
                return true;
            }
            else {
                return false;
            }
        }


        @Override
        public String getDescription() {
            return ("Arquivo de configuração xml");
        }
    }
    
    private static class MyFileFilter2 extends FileFilter{

        @Override
        public boolean accept(File pathname) {

            if(pathname.isDirectory() || pathname.isFile()){
                return true;
            }
            else {
                return false;
            }
        }


        @Override
        public String getDescription() {
            return ("Todos os arquivos");
        }
    }
}
